﻿using EyFramework.Common;
using EyFramework.Sys.Core.Service;
using EyFramework.Sys.Entity;
using System;
using System.Collections.Generic;
using System.Linq;

namespace EyFramework.Sys.Core
{
    /// <summary>
    /// 缓存所有的系统参数、系统字典、员工信息、科室信息、
    /// </summary>
    public class SysCache
    {
        private static List<SysDictData> _CACHE_DICT = null;

        private static List<SysUser> _CACHE_USERS = null;

        public static List<SysMenu> _CACHE_MENUS = null;

        public static List<SysConfig> _CACHE_CONFIGS = null;

        public static void InitCache()
        {
            ISysDictDataService sysDictService = Globals.GetService<ISysDictDataService>();
            ISysUserService sysUserService = Globals.GetService<ISysUserService>();
            ISysConfigService sysConfigService = Globals.GetService<ISysConfigService>();
            var sysMenuService = Globals.GetService<ISysMenuService>();
            _CACHE_DICT = sysDictService.GetAllDicts(true);
            _CACHE_USERS = sysUserService.SelectAllUsers();
            _CACHE_CONFIGS = sysConfigService.SelectAllConfigs();
        }

        public static SysConfig GetCacheConfig(string configKey)
        {
            return _CACHE_CONFIGS.FirstOrDefault(p => p.CONFIG_KEY == configKey);
        }

        public static List<SysDictData> GetCacheSysDict(string dictType, bool addAll = false)
        {
            List<SysDictData> dicts = new List<SysDictData>();
            if (addAll)
            {
                dicts.Add(new SysDictData()
                {
                    DICT_CODE = -1,
                    DICT_VALUE = "",
                    DICT_LABEL = "全部"
                });
            }
            var dict = _CACHE_DICT.Where(p => p.DICT_TYPE == dictType).ToList();
            if (dict != null)
            {
                dicts.AddRange(dict);
            }
            return dicts;
        }

        /// <summary>
        /// 获取字典的值
        /// </summary>
        /// <param name="dictCode"></param>
        /// <param name="dictType"></param>
        /// <param name="i">0，输出label</param>
        /// <returns></returns>
        public static string GetDictVal(string dictCode, string dictType)
        {
            var dictlist = GetCacheSysDict(dictType);
            var dict = dictlist.FirstOrDefault(p => p.DICT_VALUE == dictCode);
            return dict == null ? "" : dict.DICT_LABEL;
        }

        public static string GetDictValue(string dictCode, string dictType)
        {
            var dictlist = GetCacheSysDict(dictType);
            var dict = dictlist.FirstOrDefault(p => p.DICT_LABEL == dictCode);

            return dict == null ? "" : dict.DICT_VALUE;
        }

        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static SysUser GetSysUserById(string userId)
        {
            var sysUser = _CACHE_USERS.FirstOrDefault(p => p.USER_ID == userId);
            if (sysUser == null)
            {
                return new SysUser();
            }
            return sysUser;
        }
    }
}